/*****************************************************************************
 *  CommLayerLib - BluetoothLayer.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: BluetoothLayer.java 41 2012-10-11 16:35:35Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer.bluetooth;

import it.picciux.commlayer.CommLayer;
import it.picciux.commlayer.ConfigValue;

public abstract class BluetoothLayer extends CommLayer {
	public class Settings extends CommLayer.Settings {

		protected String address = "";
		protected String name = "";
		protected int serviceClass = 0;
		protected int majorDeviceClass = 0;
		protected int minorDeviceClass = 0;
		
		public boolean setConfigValue(ConfigValue v) {
			if (v.getKey().equalsIgnoreCase("name"))
				name = v.getValueString();
			else if (v.getKey().equalsIgnoreCase("address"))
				address = v.getValueString();
			else if (v.getKey().equalsIgnoreCase("url"))
				setUrl(v.getValueString());
			else if (v.getKey().equalsIgnoreCase("serviceclass"))
				serviceClass = v.getValueInt();
			else if (v.getKey().equalsIgnoreCase("majordeviceclass"))
				majorDeviceClass = v.getValueInt();
			else if (v.getKey().equalsIgnoreCase("minordeviceclass"))
				minorDeviceClass = v.getValueInt();
			else
				return false;
			
			return true;
		}

		public ConfigValue getNextConfig() {
			ConfigValue ret = null;
			
			switch(fieldIndex) {
				case 0:
					ret = new ConfigValue("name", name);
					break;
				case 1:
					ret = new ConfigValue("address", address);
					break;
				case 2:
					ret = new ConfigValue("serviceclass", serviceClass);
					break;
				case 3:
					ret = new ConfigValue("majordeviceclass", majorDeviceClass);
					break;
				case 4:
					ret = new ConfigValue("minordeviceclass", minorDeviceClass);
					break;
				default:
					return null;
					
			}
			
			fieldIndex++;
			return ret;
		}

		public CommLayer.Settings clone() {
			Settings s = new Settings();
			s.address = address;
			s.name = name;
			s.serviceClass = serviceClass;
			s.majorDeviceClass = majorDeviceClass;
			s.minorDeviceClass = minorDeviceClass;
			return s;
		}

		/**
		 * @return the address
		 */
		public String getAddress() {
			return address;
		}

		/**
		 * @param address the address to set
		 */
		public void setAddress(String address) {
			this.address = address;
		}

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}
		
		public String getUrl() {
			return name + "@" + address;
		}
		
		public void setUrl(String url) {
			int i = url.indexOf('@');
			if (i == -1) {
				name = url;
				address = url;
			} else {
				name = url.substring(0, i);
				address = url.substring(i + 1);
			}
		}

		/**
		 * @return the serviceClass
		 */
		public int getServiceClass() {
			return serviceClass;
		}

		/**
		 * @param serviceClass the serviceClass to set
		 */
		public void setServiceClass(int serviceClass) {
			this.serviceClass = serviceClass;
		}

		/**
		 * @return the majorDeviceClass
		 */
		public int getMajorDeviceClass() {
			return majorDeviceClass;
		}

		/**
		 * @param majorDeviceClass the majorDeviceClass to set
		 */
		public void setMajorDeviceClass(int majorDeviceClass) {
			this.majorDeviceClass = majorDeviceClass;
		}

		/**
		 * @return the minorDeviceClass
		 */
		public int getMinorDeviceClass() {
			return minorDeviceClass;
		}

		/**
		 * @param minorDeviceClass the minorDeviceClass to set
		 */
		public void setMinorDeviceClass(int minorDeviceClass) {
			this.minorDeviceClass = minorDeviceClass;
		}
		
	}
	
	public static final int BLUETOOTH_NOT_AVAIL		= (BLUETOOTH * 100) + 0;
	public static final int BLUETOOTH_DISABLED		= (BLUETOOTH * 100) + 1;
	
	public BluetoothLayer() {
		settings = new Settings();
	}

	public boolean canScan() {
		return true;
	}

	public int getType() {
		return CommLayer.BLUETOOTH;
	}

	public it.picciux.commlayer.CommLayer.Settings getSettings() {
		return settings;
	}
}
